Repository navigation
ci: publish to NuGet.org with Trusted Publishing - #21
Merged
Merged
Conversation
- Exchange the GitHub OIDC token for a temporary NuGet API key with NuGet/login instead of using the NUGET_API_KEY secret - Grant id-token: write to the publish job only and drop the unused packages: write permission - Allow publishing the packages of an existing release manually with workflow_dispatch and a tag input - Pass the release tag to the script through an environment variable - Describe the release process in the README
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR switches the NuGet.org publishing in
release-publish.ymlfrom a stored API key to Trusted Publishing. The workflow exchanges a GitHub OIDC token for a NuGet API key that is valid for one hour, so the repository no longer stores a long-lived NuGet API key that can leak or expire.The release of 3.2.3 shows why: its publish job failed because the stored key had expired, and 3.2.3 is not on NuGet.org yet.
The workflow can now also be started manually for an existing release. This is how 3.2.3 will be published after the merge, because a re-run of the failed job would use the old workflow and the expired key again.
Changes
release-publish.ymlNuGet login (Trusted Publishing)runsNuGet/login@v1withuser: ${{ secrets.NUGET_USER }}. The push step uses its outputsteps.nuget-login.outputs.NUGET_API_KEYinstead ofsecrets.NUGET_API_KEY. The login runs right before the push, because the temporary key is valid for one hour.id-token: write, whichNuGet/loginneeds to request the GitHub OIDC token, andcontents: readto download the release assets. The workflow-level permissions are reduced tocontents: read. The unusedpackages: writepermission is removed; the workflow does not publish to GitHub Packages.workflow_dispatchtrigger with a requiredtaginput. It downloads and publishes the packages of the given GitHub release, for examplegh workflow run release-publish.yml -f tag=v3.2.3.--skip-duplicatestays, so versions that are already on NuGet.org are skipped.RELEASE_TAGenvironment variable instead of an expression inside the script, so a manually entered tag cannot inject shell code.README
Setup on NuGet.org and GitHub
These steps need an account with access to the
MyCSharporganization on NuGet.org and admin access to this repository.On NuGet.org, open Trusted Publishing and add a policy for GitHub Actions:
MyCSharp(the organization that owns both packages)mycsharpHttpClientHintsrelease-publish.yml(file name only)Add the repository secret
NUGET_USERwith the NuGet.org profile name of the member who created the policy. This is the user name, not the email address and not the organization name.Merge this PR.
Publish 3.2.3:
gh workflow run release-publish.yml -f tag=v3.2.3.After a successful publish, delete the
NUGET_API_KEYrepository secret and the expired key on NuGet.org.Notes for reviewers
userfor an organization policy: The NuGet docs and theNuGet/loginREADME do not say which account name to use when an organization owns the policy. Other projects with organization-owned packages report that the profile name of the member who created the policy works. If the login step fails with a policy error, check this value first.nugetwith protection rules, for example to allow deployments only frommainand tags. Its name would then be added to the job and to the policy.NUGET_API_KEYsecret. Use the manual trigger instead.Testing
releaseandworkflow_dispatch, the job permissionscontents: readandid-token: write, and the login step before the push step.v3.2.3and a lookup of the package versions on NuGet.org.